package com.feng.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.commonutils.JwtUtils;
import com.feng.commonutils.R;
import com.feng.commonutils.vo.CourseVo;
import com.feng.commonutils.vo.UcenterMemberVo;
import com.feng.eduorder.client.EduClient;
import com.feng.eduorder.client.UcenterClient;
import com.feng.eduorder.entity.Order;
import com.feng.eduorder.mapper.OrderMapper;
import com.feng.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-03-30
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private UcenterClient ucenterClient;
    @Autowired
    private EduClient eduClient;
    @Transactional
    @Override
    public R saveOrder(String courseId, HttpServletRequest request) {
        //调用jwt工具类
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            //如果为空，则没登陆
            return R.error().message("请先登录");
        }
        //根据ucenterClient远程调用ucenter接口
        UcenterMemberVo ucenterMemberVo = ucenterClient.GetMemberInfoById(memberId);
        //根据eduClient远程调用edu接口
        CourseVo courseInfos = eduClient.getCourseInfos(courseId);
        Order order=new Order();
        //设置用户id
        order.setMemberId(memberId);
        //设置课程id
        order.setCourseId(courseInfos.getId());
        //设置课程标题
        order.setCourseTitle(courseInfos.getTitle());
        //设置价格
        order.setTotalFee(courseInfos.getPrice());
        //设置讲师名称
        order.setTeacherName(courseInfos.getTeachername());
        //设置课程封面
        order.setCourseCover(courseInfos.getCover());
        //设置手机号
        order.setMobile(ucenterMemberVo.getMobile());
        //设置昵称
        order.setNickname(ucenterMemberVo.getNickname());
        //设置订单号
        String orderNo = OrderNoUtil.getOrderNo();
        order.setOrderNo(orderNo);
        //支付状态（0未支付)
        order.setStatus(0);
        //设置支付方式
        order.setGmtCreate(new Date());
        order.setGmtModified(new Date());
        order.setPayType(1);
        this.save(order);
        //返回订单号
        return R.ok().data("orderId",orderNo);
    }

    @Override
    public R getOrderInfo(String orderId) {
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getOrderNo,orderId);
        Order order = this.getOne(queryWrapper);
        return R.ok().data("item",order);
    }

    @Override
    public Boolean getCourseBuy(String courseId,String memberId) {
        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getCourseId,courseId);
        queryWrapper.eq(Order::getMemberId,memberId);
        queryWrapper.eq(Order::getStatus,1);  //1代表已经支付
        int count = this.count(queryWrapper);
        if(count>0){
            return true;
        }
        else{
            return false;
        }
    }
}
